﻿using System;
using HarmonyLib;
using UnityEngine;
using Config;
using FrameWork.ModSystem;
using GameData.Domains.Item.Display;
using GameData.Domains.Item;

namespace CricketCheat
{
    [HarmonyPatch(typeof(UI_CatchCricket), "InitCatchPlace")]
    class InitCatchPlace_Path
    {
        public static void Postfix()
        {
            if (!Main.ShowCricket)
            {
                return;
            }
            UI_CatchCricket ui = UIElement.CatchCricket.UiBaseAs<UI_CatchCricket>();
            Traverse uiTrv = Traverse.Create(ui);
            UI_CatchCricket.CricketPlaceInfo[] placeList = uiTrv.Field("_catchPlaceList").GetValue<UI_CatchCricket.CricketPlaceInfo[]>();
            RectTransform root = uiTrv.Field("_catchPlaceRoot").GetValue<RectTransform>();
            GameObject inst = ui.CGet<RectTransform>("Jar").GetChild(0).gameObject;
            for (int i = 0; i < 21; i++)
            {
                var parent = root.GetChild(i).transform;
                var cricketTrans = parent.Find("cricketView");
                if (cricketTrans == null)
                {
                    GameObject obj = GameObject.Instantiate(inst);
                    obj.name = "cricketView";
                    obj.SetActive(true);
                    cricketTrans = obj.transform;
                    cricketTrans.SetParent(parent, false);
                    cricketTrans.localScale = new Vector3(0.03f, 0.03f, 0.1f);
                    cricketTrans.localPosition = new Vector3(100, -120, 0);
                }
                
                CricketView component = cricketTrans.GetComponent<CricketView>();
                UI_CatchCricket.CricketPlaceInfo info = placeList[i];
                sbyte level = Math.Max(CricketParts.Instance[info.CricketColorId].Level, CricketParts.Instance[info.CricketPartsId].Level);
                short templateId = CalcGrade(info.CricketColorId, info.CricketPartsId);
                CricketPartsItem partsCfg = CricketParts.Instance[templateId];
                component.Inited = false;
                component.SetCricketData(info.CricketColorId, info.CricketPartsId, false, null, false);

                string corlor = CricketParts.Instance[info.CricketColorId].Color;
                string str;
                if (!corlor.IsNullOrEmpty())
                {
                    str = string.Format("<color=#{1}>{0}</color>Lv.{2}", partsCfg.Name[0], corlor, level);
                } else
                {
                    str = string.Format("{0}Lv.{1}", partsCfg.Name[0], level);
                }
                
                var textTrans = parent.Find("cricketName");
                if (textTrans == null)
                {
                    var text = GameObjectCreationUtils.UGUICreateTMPText(parent, str);
                    text.fontSize = 20;
                    text.transform.localPosition = new Vector3(102, -173);
                    text.transform.gameObject.name = "cricketName";
                } else
                {
                    textTrans.GetComponent<TMPro.TextMeshProUGUI>().text = str;
                }
                parent.Find("PlaceImg").gameObject.SetActive(false);
            }
        }

        private static sbyte CalcGrade(short colorId, short partId)
        {
            bool flag = colorId < 0;
            sbyte result;
            if (flag)
            {
                result = 0;
            }
            else
            {
                sbyte value = CricketParts.Instance[colorId].Level;
                bool flag2 = partId > 0;
                if (flag2)
                {
                    value = Math.Max(CricketParts.Instance[partId].Level, value);
                }
                result = value;
            }
            return result;
        }
    }
}
